
//
//  GuiDangViewController.m
//  数据存储
//
//  Created by Clement_Gu on 16/3/14.
//  Copyright © 2016年 小白. All rights reserved.
//

#import "GuiDangViewController.h"
#import "StudentModel.h"
@interface GuiDangViewController ()
@property (nonatomic,strong) NSMutableArray *stuList;
@property (weak, nonatomic) IBOutlet UITextField *stuID;
@property (weak, nonatomic) IBOutlet UITextField *stuName;
@property (weak, nonatomic) IBOutlet UITextField *stuAge;
@end

@implementation GuiDangViewController
/**重写getter方法 懒加载*/
-(NSMutableArray *)stuList
{
    if (_stuList == nil)
    {
        //判断文件是否存在 如果之前已有存档文件 则从中解档出来以前的数据
        NSFileManager  *fm = [NSFileManager defaultManager];
        if ([fm fileExistsAtPath:[self dataPath]]) {
            //解档
            //1.读取数据（二进制数据）
            NSData *data = [NSData  dataWithContentsOfFile:[self dataPath]];
            //创建解档对象
            NSKeyedArchiver  *unArch = [[NSKeyedArchiver alloc]initForWritingWithMutableData:data];
            //3从解档对象中解档Student（和归档时使用相同的key）
            _stuList = [unArch decodeObjectForKey:@"students"];
            //4完成解档
            [unArch finishEncoding];
        }
        else
        {
            //没有存档文件则新建一个空数组
            _stuList = [NSMutableArray array];
        
        }
    }
    return _stuList;
}
-(NSString *)dataPath
{
    NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    NSString *filePath = [path stringByAppendingPathComponent:@"students.arch"];
    return filePath;
}
-(void)saveData
{
    //创建可变数据对象 用于归档存入
    NSMutableData *mData = [NSMutableData data];
    //创建归档对象
    NSKeyedArchiver *arch = [[NSKeyedArchiver alloc]initForWritingWithMutableData:mData];
    //归档数组对象（数组本身支持归档 数组的元素也要支持归档）
    [arch encodeObject:self.stuList forKey:@"Students"];
    //完成归档
    [arch finishEncoding];
    //数据存入
    [mData writeToFile:[self dataPath] atomically:YES];


}



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
- (IBAction)guiDAddBtn:(id)sender {
    //1,将信息存储到对象中
    StudentModel *m = [[StudentModel alloc]initWithId:self.stuID.text andName:self.stuName.text andAge:self.stuAge.text];
    //2将对象加入到数组中
    [self.stuList   addObject:m];
    //保存
    [self saveData];
    
    
    
}
- (IBAction)guiDDeleteBtn:(id)sender {
    //1遍历学生数组
    for(StudentModel *m in self.stuList)
    {
        //2.如果学号相同
        if ([m.stuId isEqualToString:self.stuID.text]) {
            //3将该学生从字典中删除
            [self.stuList removeObject:m];
            break;
        }
    
    }
    // 4. 保存数组
    [self saveData];
}
- (IBAction)guiDMotifyBtn:(id)sender {
    //1遍历学生数组
    for (StudentModel *m in self.stuList) {
        //如果学号相同
        if ([m.stuId isEqualToString:self.stuID.text]) {
            //修改该学生信息
            m.stuName = self.stuName.text;
            m.stuAge = self.stuAge.text;
            break;
            
        }
    }
    //4保存数组
    [self saveData];
    
}
- (IBAction)guiDResarchBtn:(id)sender {
    //遍历学生数组
    for (StudentModel *m in self.stuList) {
            //如果学号相同
        if ([m.stuId isEqualToString:self.stuID.text]) {
            //3将查询到的信息显示到界面
            self.stuName.text = m.stuName;
            self.stuAge.text = m.stuAge;
            
            break;
        }
    }
    
    
}
- (IBAction)guiDFindAllBtn:(id)sender {
    for (StudentModel *m  in self.stuList) {
        NSLog(@"%@",m);
    }
    
    
}

@end
